package net.sapfree.display.tags.person;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import net.sapfree.common.Constants;
import net.sapfree.core.db.DBConnectionProvider;
import net.sapfree.core.db.DataBaseExecutor;
import net.sapfree.core.db.Record;
import net.sapfree.core.db.RecordSet;
import net.sapfree.display.tags.core.LoopTagSupport;

import org.apache.log4j.Logger;

public class PersonListTag extends LoopTagSupport {
	private static Logger log = Logger.getLogger(PersonListTag.class.getClass());
	private RecordSet personList=null;
	private String filterName;	//查询条件对应的数据库字段
	private String filterValue;	//查询条件对应的数据值
	private int count=-1;
	@Override
	protected boolean hasNext() throws JspException {
		return (personList != null)&&(step<end);
	}

	@Override
	protected Object next() throws JspException {
		Record r=personList.get(step);
		step++;
		return r;
	}

	 
	
	@Override
	protected void prepare() throws JspException {
		ServletRequest request = pageContext.getRequest();
		Connection conn =null;

		try {
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SAPFREE);
			DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
			personList=dbe.find(getPersonListSql());
			System.out.println(personList.toString());
			pageContext.setAttribute("PersonListTag", this, PageContext.PAGE_SCOPE);
			end=personList.size();
		 	step=0;
		} catch (SQLException e) {
			log.error("在执查找人员列表时出错",e);
		}finally{
			try{
				 conn.close();
			}catch(Exception e){}
		}
	}
	
	private String getPersonListSql(){
		String[] filternames = null;
		String[] filtervalues= null;
		if(filterName!=null && !"".equals(filterName)){
			filternames = filterName.split(",");
			filtervalues=filterValue.split(",");
		}
		StringBuffer sql=new StringBuffer();
		//如果没有限制条件，则返回所有的记录
		sql.append("SELECT * FROM PERSONINFO where PI_USERSTATUS='1' " );
		System.out.println(sql.toString());
		if(filternames!=null){
			
			for(int i=0;i<filternames.length;i++){
				sql.append(" and ( ");
				for(int v=0;v<filtervalues.length;v++){
					sql.append(filterName +" like '%"+filtervalues+"%' OR ");
				}
				sql.delete(sql.length()-3, sql.length());//删除最后的OR
				sql.append(" )"); 
			}
		}
		System.out.println(sql.toString());
		
		if(count!=-1){
			sql.append(" limit 0,").append(count);
		}
		System.out.println(sql.toString());
		return sql.toString();
	}

	/**
	 * @return the filterName
	 */
	public String getFilterName() {
		return filterName;
	}

	/**
	 * @param filterName the filterName to set
	 */
	public void setFilterName(String filterName) {
		this.filterName = filterName;
	}

	/**
	 * @return the filterValue
	 */
	public String getFilterValue() {
		return filterValue;
	}

	/**
	 * @param filterValue the filterValue to set
	 */
	public void setFilterValue(String filterValue) {
		this.filterValue = filterValue;
	}

	/**
	 * @return the count
	 */
	public int getCount() {
		return count;
	}

	/**
	 * @param count the count to set
	 */
	public void setCount(int count) {
		this.count = count;
	}

}
